package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.az.AzkabanSummary;
import com.niodata.dp.monitor.sharelib.entity.hadoop.HdfsPerformance;
import com.niodata.dp.monitor.sharelib.entity.hadoop.HdfsSummary;
import com.niodata.dp.monitor.sharelib.entity.hadoop.YarnSummary;
import com.niodata.dp.monitor.sharelib.entity.hbase.HbaseSummary;
import com.niodata.dp.monitor.sharelib.entity.hbase.RegionServerInfo;
import com.niodata.dp.monitor.sharelib.entity.presto.PrestoSummary;
import com.niodata.dp.monitor.sharelib.entity.zookeeper.ZkSummary;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * Created by ndp on 17-12-27.
 */
@Component
public class MetricsMonitorService {

  @Autowired
  @Qualifier("monitorRestTemplate")
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  public HdfsSummary getNowHdfsSummary() {
    String url = String.format("http://%s:%s/monitor/metrics/hdfs/summary/now", server, port);
    return restTemplate.getForEntity(url, HdfsSummary.class).getBody();
  }

  public List<HdfsSummary> getHdfsSummary(String startTime, String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/metrics/hdfs/summary?startTime=%s&stopTime=%s", server,
                port,
                startTime, stopTime);
    return Arrays.asList(restTemplate.getForEntity(url, HdfsSummary[].class).getBody());
  }

  public HdfsPerformance getNowHdfsPerformance() {
    String url = String.format("http://%s:%s/monitor/metrics/hdfs/performance/now", server, port);
    return restTemplate.getForEntity(url, HdfsPerformance.class).getBody();
  }

  public List<HdfsPerformance> getHdfsPerformance(String startTime, String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/metrics/hdfs/performance?startTime=%s&stopTime=%s", server,
                port,
                startTime, stopTime);
    return Arrays.asList(restTemplate.getForEntity(url, HdfsPerformance[].class).getBody());
  }

  public YarnSummary getNowYarnSummary() {
    String url = String.format("http://%s:%s/monitor/metrics/yarn/summary/now", server, port);
    return restTemplate.getForEntity(url, YarnSummary.class).getBody();
  }

  public List<YarnSummary> getYarnSummary(String startTime, String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/metrics/yarn/summary?startTime=%s&stopTime=%s", server,
                port,
                startTime, stopTime);
    return Arrays.asList(restTemplate.getForEntity(url, YarnSummary[].class).getBody());
  }

  public HbaseSummary getNowHbaseSummary() {
    String url = String.format("http://%s:%s/monitor/metrics/hbase/summary/now", server, port);
    return restTemplate.getForEntity(url, HbaseSummary.class).getBody();
  }

  public List<RegionServerInfo> getHbaseSummary(String startTime, String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/metrics/hbase/summary?startTime=%s&stopTime=%s", server,
                port,
                startTime, stopTime);
    return Arrays.asList(restTemplate.getForEntity(url, RegionServerInfo[].class).getBody());
  }

  public List<RegionServerInfo> getHbaseSummaryByHost(String hostName, String startTime,
        String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/metrics/hbase/summary?hostName=%s&startTime=%s&stopTime=%s",
                server, port, hostName,
                startTime, stopTime);
    return Arrays.asList(restTemplate.getForEntity(url, RegionServerInfo[].class).getBody());
  }

  public PrestoSummary getNowPrestoSummary() {
    String url = String.format("http://%s:%s/monitor/metrics/presto/summary/now", server, port);
    return restTemplate.getForEntity(url, PrestoSummary.class).getBody();
  }

  public ZkSummary getNowZkSummary() {
    String url = String.format("http://%s:%s/monitor/metrics/zk/summary/now", server, port);
    return restTemplate.getForEntity(url, ZkSummary.class).getBody();
  }

  public AzkabanSummary getAzkabanSummary(String startTime, String stopTime) {
    String url = String
          .format("http://%s:%s/monitor/task/azkaban/status?startTime=%s&stopTime=%s", server,
                port,
                startTime, stopTime);
    return restTemplate.getForEntity(url, AzkabanSummary.class).getBody();
  }
}
